
-----------------------------------------------------------------------------
-- 设置界面
--
---------------
-- Author: 筱迪
-- Date: 2020.12.21
-----------------------------------------------------------------------------
require "Common/define"
require "Logic/Config"

UICommonSet = UIBase.New(PanelResNames.UICommonSet)
local this = UICommonSet
local m_account_support = false
local speedData = nil

function UICommonSet.Init()
    UIBase.Init(this)
end

function UICommonSet:OnCreate()
    self:RegisterEvent(Config.EventType.Account_center_unread_count, function()
        if m_account_support then
            self.sdk_red:GetController("c1").selectedIndex = logicMgr.SDKManager.GetAccountCenterCount() > 0 and 2 or 0
        end
    end)

    self.Slider_Music = self:GetChild("Slider_Music")
    self.Slider_Sound = self:GetChild("Slider_Sound")
    self.Slider_Voice = self:GetChild("Slider_Voice")

    self.btnGiftCode = self:GetChild("n101")
    self.codeInput = self:GetChild("n99/n1")
    --基本上是为iOS过审设计，指向过审渠道，并且需要关闭兑换码
    if ( (Application.platform == RuntimePlatform.IPhonePlayer and AppConst.forceChannel > 0) or Application.platform == RuntimePlatform.Android ) and Config.RemoteConfig.ExchangeCodeDisable == 1 then
        self.inputEdge = self:GetChild("n99")
        self.duihuanTxt = self:GetChild("n96")
        self.btnGiftCode.visible = false
        self.inputEdge.visible = false
        self.duihuanTxt.visible = false
    end
    self.btnSelectServer = self:GetChild("btnSelectServer")
    self.Btn_ChangeVoice = self:GetChild("Btn_ChangeVoice")
    self.BtnSwitch=self:GetChild("swith")
    self.btnCG = self:GetChild("btnBeginningCg")
    self.btnHideSelfDamage = self:GetChild("Btn_HideOurDamage")
    self.btnMemory = self:GetChild("Btn_Memory")
    self.btnPrivacyAgreement = self:GetChild("Btn_PrivacyAgreement")
    self.Btn_AccountDelete   = self:GetChild("Btn_AccountDelete  ")
    self.btnUserAgreement = self:GetChild("Btn_UserAgreement")
    self.btnSwitchAccount = self:GetChild("Btn_ChangeAccount")
    self.Btn_ChangeLanguage = self:GetChild("Btn_ChangeLanguage")
    self.btn_CheckGameData = self:GetChild("Btn_Repair")
    
    self.Btn_SDK = self:GetChild("Btn_SDK")
    self.sdk_red = self.Btn_SDK:GetChild("UIRedDots")
    self.speedList = self:GetChild("listSpeed")
    self:SetDynamicList(self.speedList,self.SetSpeedItem,nil,true)
    speedData = {}
    speedData = dataTable.getGlobalString(664)
    self.speedList.numItems = #speedData

    m_account_support = LoginSDK.GetInstance():GetSupportAccountCenter()
    if Application.platform == RuntimePlatform.IPhonePlayer then
        m_account_support = true;
    end
    if self.Btn_SDK  then
        self.Btn_SDK.visible = m_account_support
        if m_account_support then
            self.sdk_red:GetController("c1").selectedIndex = SDKManager.GetAccountCenterCount() > 0 and 2 or 0
        end
    end

    self:InitView()
    self:BindEvent()
end

function UICommonSet:SetSpeedItem(index,obj)
    local dec = obj:GetChild("title")
    dec.text = "x"..speedData[index]
    local speedIndex = tonumber(GetPlayerPrefs("AddUISpeed") or 1)
    if speedIndex == speedData[index] then
        obj.selected = true
    else
        obj.selected = false
    end
end

function UICommonSet:ClickSpeedItem(context)
    local _obj = context.data
    local index = self.speedList:GetChildIndex(_obj)+1
    Time.timeScale = speedData[index]
    SetPlayerPrefs("AddUISpeed",speedData[index])
end

function UICommonSet:InitView()
    self.Slider_Music.value = AudioManager.MusicVolume
    self.Slider_Sound.value = AudioManager.SoundVolume
    self.Slider_Voice.value = AudioManager.VoiceVolume
end
function UICommonSet:OnEnable()
    local hide = GetPlayerPrefs("hideSelfDamage")
    if hide then
        self.btnHideSelfDamage.selected = tonumber(hide) == 1
    else
        self.btnHideSelfDamage.selected = true
    end
    hide = GetPlayerPrefs("hideMemoryClick")
    if hide then
        self.btnMemory.selected = tonumber(hide) == 1
    else
        self.btnMemory.selected = true
    end
end
function UICommonSet:OnDisable()
    self:clearCode();
end

function UICommonSet:clearCode()
    self.codeInput.text = "";
end

function UICommonSet:BindEvent()
    self:RegisterOnClick(self.btnSelectServer, self.OnSelectServer)
    self:RegisterOnClick(self.Btn_ChangeVoice, self.onClickChangeVoice)
    self:RegisterOnClick(self.BtnSwitch, self.OnClickSwitch)
    self:RegisterOnClick(self.btnGiftCode, self.ClickGiftCode)
    self:RegisterOnClick(self.btnCG,self.PlayCG)
    self:RegisterOnClick(self.btnHideSelfDamage, self.onHideDamageClick)
    self:RegisterOnClick(self.btnMemory, self.onHideMemoryClick)
    self:RegisterOnClick(self.btnPrivacyAgreement,self.OnPrivacyAgreement)
    self:RegisterOnClick(self.Btn_AccountDelete,self.OnAccountDelete)
    self:RegisterOnClick(self.btnUserAgreement,self.OnUserAgreement)
    self:RegisterOnClick(self.btnSwitchAccount,self.OnSwitchAccount)
    self:RegisterOnChanged(self.Slider_Music, self.onMusicVolumeChange)
    self:RegisterOnChanged(self.Slider_Sound, self.onSoundVolumeChange)
    self:RegisterOnChanged(self.Slider_Voice, self.onVoiceVolumeChange)
    self:RegisterTouchBegin(self.Slider_Voice:GetChild("grip"),self.onTouchStartVoice)
    self:RegisterTouchEnd(self.Slider_Voice,self.onTouchEndVoice)
    self:RegisterEvent(Config.EventType.ReceiveCode,self.clearCode);
    self:RegisterOnClick(self.Btn_ChangeLanguage,self.OnClick_languageChanged)
    self:RegisterOnClick(self.Btn_SDK, self.onClick_SdkAccountCenter)
    self:RegisterOnClick(self.btn_CheckGameData, self.onClick_CheckGameData)
    self.speedList.onClickItem:Add(function(context)
        self:ClickSpeedItem(context)
    end)
end

function UICommonSet:OnPrivacyAgreement()
    showUI(PanelNames.UIPrivacy,2)
end

function UICommonSet:OnAccountDelete()
    showUI(PanelNames.UIPrivacy,3)
end

function UICommonSet:OnUserAgreement()
    showUI(PanelNames.UIPrivacy,1)
end
function UICommonSet:OnSwitchAccount()
    -- AudioManager.Stop(AudioManager.LastPlayInteractionEventName,AudioManager.LastPlayInteractionGO, AudioManager.WwiseFlagEnum.Sound)
    AudioManager.Stop("Stop_All", AudioManager.LastPlayInteractionGO, AudioManager.WwiseFlagEnum.Sound)
    print("On SwitchAccount click LoginOutU8SDK")
    LoginSDK.GetInstance():LoginOutU8SDK()
    logicMgr.LoginManager.SetAutoLogin(false)
    Game.ResetStart(false)
    Game.stopHeart()
    SDKManager.IMDisconnect()
end

function UICommonSet:PlayCG()
    showUI(PanelNames.UIPlayVideo,AudioManager.VideoInfo[1])
end

function UICommonSet:OnSelectServer()
    showUI(PanelNames.UIServer)
end

function UICommonSet:onClickChangeVoice()
    showUI(PanelNames.Page_VoiceChangeLanguage)
end

function UICommonSet:onHideDamageClick()
    FightManager.hideSelfDamage = self.btnHideSelfDamage.selected
    local hide = 0
    if FightManager.hideSelfDamage then
        hide = 1
    end
    SetPlayerPrefs("hideSelfDamage", hide)
end

function UICommonSet:onHideMemoryClick()
    local hide = 0
    if self.btnMemory.selected then
        hide = 1
    end
    SetPlayerPrefs("hideMemoryClick", hide);
end

function UICommonSet:OnClickSwitch()
    UIHeadInfo.OnChangeAccClick()
end

function UICommonSet:onMusicVolumeChange(value)
    local _musicVolume = Mathf.Ceil(value.sender.value)
    AudioManager.SetVolume(_musicVolume,AudioManager.WwiseFlagEnum.Music)
end

function UICommonSet:onSoundVolumeChange(value)
    local _soundVolume = Mathf.Ceil(value.sender.value)
    AudioManager.SetVolume(_soundVolume,AudioManager.WwiseFlagEnum.Sound)
    AudioManager.Play(1001)
end
function UICommonSet:onVoiceVolumeChange(value)
    local _voiceVolume = Mathf.Ceil(value.sender.value)
    AudioManager.SetVolume(_voiceVolume,AudioManager.WwiseFlagEnum.Voice)
end

--播放默认立绘语音
function UICommonSet:onTouchStartVoice()
    AudioManager.PlayInteractionVoice(logicMgr.HeroManager.GetRoleHeadID(),0,AudioManager.InteractionTagTable[2].Tag)
end
function UICommonSet:onTouchEndVoice()
    AudioManager.PlayInteractionVoice(logicMgr.HeroManager.GetRoleHeadID(),0,AudioManager.InteractionTagTable[2].Tag,nil,nil,true)
end

function UICommonSet:ClickGiftCode()
    local InputText = self.codeInput.text
    if InputText == "" then
        UISysTips.AddMsg("输入不能为空")
        return
    end
    local req = ReqCodeMessage.New();
    req.code =  InputText;
    sendReqMessage(req);
end
function UICommonSet:OnClick_languageChanged()
    -- showUI(PanelResNames.UILanguageSetting)
    UISysTips.AddMsg(getLanguage("lbs_func_not_open"))
end

function UICommonSet:onClick_SdkAccountCenter()
    LoginSDK.GetInstance():ShowAccountCenter()
end

function UICommonSet:onClick_CheckGameData()
    logicMgr.LoginManager.showRepairGameTips();
end

return UICommonSet;